import defaultSettings from '@/settings'
import { getConfig } from '@/api/system/setting'
const { showSettings, fixedHeader, sidebarLogo } = defaultSettings

const state = {
  showSettings: showSettings,
  fixedHeader: fixedHeader,
  sidebarLogo: sidebarLogo,
  mainTit: '',
  mainColor: ''
}

const mutations = {
  CHANGE_SETTING: (state, { key, value }) => {
    // eslint-disable-next-line no-prototype-builtins
    if (state.hasOwnProperty(key)) {
      state[key] = value
    }
  },
  MAINTIT_SETTING: (state, mainTit) => {
    state.mainTit = mainTit
  },
  MAINCOLOR_SETTING: (state, mainColor) => {
    state.mainColor = mainColor
  }
}

const actions = {
  changeSetting({ commit }, data) {
    commit('CHANGE_SETTING', data)
  },
  changeMainSet({ commit }) {
    return new Promise((resolve, reject) => {
      getConfig().then(res => {
        if (res.status === 200 && res.data) {
          commit('MAINTIT_SETTING', res.data.title)
          commit('MAINCOLOR_SETTING', res.data.colour)
          resolve(res.data)
        }
      }).catch(error => {
        reject(error)
      })
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

